<?php

namespace app\admin\model\staff;

use app\common\model\Service;
use think\Db;
use think\Model;
use traits\model\SoftDelete;

class Staff extends Model
{

    use SoftDelete;

    

    // 表名
    protected $name = 'staff';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'integer';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = 'deletetime';

    // 追加属性
    protected $append = [
//        'starttime_text',
//        'endtime_text',
        'check_state_text',
        'is_share_text',
        'state_text',
        'service_text',
        'service1_text',
        'city_text',
        'province_text',
    ];
    

    protected static function init()
    {
        self::afterInsert(function ($row) {
            $pk = $row->getPk();
            $row->getQuery()->where($pk, $row[$pk])->update(['weigh' => $row[$pk]]);
        });
    }

    public function getCityTextAttr($value, $data)
    {
        $info = Db::name('area')->where(['id' => $data['city']])->find();
        return $info ? $info['name'] : '';
    }

    public function getProvinceTextAttr($value, $data)
    {
        $info = Db::name('area')->where(['id' => $data['province']])->find();
        return $info ? $info['name'] : '';
    }
    
    public function getCheckStateList()
    {
        return ['0' => __('Check_state 0'), '1' => __('Check_state 1'), '2' => __('Check_state 2')];
    }

    public function getIsShareList()
    {
        return ['0' => __('Is_share 0'), '1' => __('Is_share 1')];
    }

    public function getStateList()
    {
        return ['0' => __('State 0'), '1' => __('State 1'), '2' => __('State 2')];
    }


//    public function getStarttimeTextAttr($value, $data)
//    {
//        $value = $value ? $value : (isset($data['starttime']) ? $data['starttime'] : '');
//        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
//    }
//
//
//    public function getEndtimeTextAttr($value, $data)
//    {
//        $value = $value ? $value : (isset($data['endtime']) ? $data['endtime'] : '');
//        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
//    }

    public function getServiceTextAttr($value, $data)
    {
        $model = new Service();
        $res = $model->where('id','in',$data['service_ids'])->select();
        $text = [];
        foreach ($res as $item) {
            $text[] = $item['name'];
        }
//        return implode(',', $text);
        return implode(',',$text);
    }

    public function getService1TextAttr($value, $data)
    {
        $model = new Service();
        $res = $model->where('id','in',$data['service1_ids'])->select();
        $text = [];
        foreach ($res as $item) {
            $text[] = $item['name'];
        }
//        return implode(',', $text);
        return implode(',',$text);
    }
    public function getCheckStateTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['check_state']) ? $data['check_state'] : '');
        $list = $this->getCheckStateList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getIsShareTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['is_share']) ? $data['is_share'] : '');
        $list = $this->getIsShareList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getStateTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['state']) ? $data['state'] : '');
        $list = $this->getStateList();
        return isset($list[$value]) ? $list[$value] : '';
    }

//    protected function setStarttimeAttr($value)
//    {
//        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
//    }
//
//    protected function setEndtimeAttr($value)
//    {
//        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
//    }


}
